package com.zhangwoo.spider.client.process;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;

import com.meiya.common.XmlUtil;
import com.meiya.common.string.StringUtil;
import com.zhangwoo.spider.po.Conversation;
import com.zhangwoo.spider.po.UrlRequest;

/**
 * 本类主要职责为：
 * <ol>
 * <li>从页面获得一个<code>UrlRequest</code></li>
 * <li>发送该请求（此处有可能需要选择线路），获得对应源码</li>
 * <li>读取解析模板配置或页面传入XML，形成模板列表</li>
 * <li>执行解析过程，并返回解析产物（包括实体与链接两种）</li>
 * <ol>
 * 
 * @author cchen
 * 
 */
public class SpiderThreadValidation extends SpiderThread {
	private Logger logger = Logger.getLogger(SpiderThreadValidation.class.getName());
	
	private UrlRequest urlReq;
	private String xml;
	
	public SpiderThreadValidation(UrlRequest req, String xml) {
		super();
		this.urlReq = req;
		this.xml = xml;
	}

	public Object[] codeProcessValidation() {
		List<AnalyserTemplate> templates = null;
		try {
			templates = this.findAnalyserTemplate(urlReq,xml);
		} catch (Exception e) {
			logger.error(e);
		}
		if (templates == null || templates.size() == 0)
			return null;

		logger.info("running url : " + urlReq.getUrl());
		
		String html = "";
		for (int i = 0; i < 3; i++) { // 最多发送3次请求
			try {
				html = sendRequest(urlReq);
				if (!StringUtil.isEmpty(html))
					break;
			} catch (Exception e) {
				logger.error("send request url : " + urlReq.getUrl()
						+ " error! ", e);
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				logger.error("thread sleep error! ", e);
			}
		}

		logger.info("running url get html length : " + html.length());

		List<Conversation> convsResult = new ArrayList<Conversation>();
		List<UrlRequest> urlReqResult = new ArrayList<UrlRequest>();
		if (!StringUtil.isEmpty(html)) { // html 若无内容  则无需分析
			// 一个个分析模板过程，单个页面可能分析出多组数据
			for (AnalyserTemplate template : templates) {
				if (!template.isMatchTemplate(urlReq, html))
					continue;

				Document docHtml = null;
				try {
					docHtml = XmlUtil.formatToDoc(html);
				} catch (Exception e) {
					logger.error("html fromat to doc error! " + html, e);
				}

				List<Conversation> convsTemp = template.findConversations(
						urlReq, html, docHtml);
				if (convsTemp != null)
					for (Conversation convs : convsTemp) {
						if (convs.getSaveable().equals("1")) {
							convsResult.add(convs);
						}
						if (convs.getRunable().equals("1"))
							urlReqResult.add(new UrlRequest(formatUrl(
									convs.getSelfLink(), urlReq), urlReq
									.getTask()));
					}
				urlReqResult.addAll(template.findLinks(urlReq, html, docHtml));
			}
		}
		
		return new Object[]{urlReqResult,convsResult};
	}



	public static void main(String[] args) {
//		new SpiderThreadValidation().start();
//		try {
//			String t=new SpiderThread().sendRequest(new UrlRequest("http://www.5nd.com/5nd_1.htm",null));
//			System.out.println(t);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

}
